/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.basics.editor.gui.AbstractToolBar;
import com.genia.toolbox.portlet.editor.PortletEditorController;
import com.genia.toolbox.portlet.editor.model.bean.PortletInitialSettings;
import com.genia.toolbox.portlet.editor.model.document.impl.DocumentModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.ContainerPortletModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.PortletModel;
import com.genia.toolbox.web.portlet.description.ContainerPortletDescription.LayoutDirection;

/**
 * The main toolbar.
 */
@SuppressWarnings("serial")
public class PortletEditorToolBar
    extends AbstractToolBar<PortletEditorGUI, PortletEditorController, DocumentModel, PortletModel, PortletInitialSettings>
{

  /**
   * The add portlet button.
   */
  private JButton buttonAddPortlet = null;

  /**
   * The delete portlet button.
   */
  private JButton buttonDeletePortlet = null;

  /**
   * The move up portlet button.
   */
  private JButton buttonMoveUp = null;

  /**
   * The move down portlet button.
   */
  private JButton buttonMoveDown = null;

  /**
   * The move right portlet button.
   */
  private JButton buttonMoveRight = null;

  /**
   * The move left portlet button.
   */
  private JButton buttonMoveLeft = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          the gui.
   */
  public PortletEditorToolBar(PortletEditorGUI gui)
  {
    super(gui);
  }



  /**
   * Initialise the specific commands of the tool bar.
   */
  @Override
  public void initialiseSpecificCommands()
  {

    URL url = PortletEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/add.gif");
    this.buttonAddPortlet = new JButton(new ImageIcon(url));
    this.buttonAddPortlet.addActionListener(this);
    this.buttonAddPortlet.setToolTipText(this.getGui().getController().notifyTranslation("com.genia.portlet.editor.gui.MainToolBar.buttonAddPortlet"));
    this.add(this.buttonAddPortlet);

    url = PortletEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/delete.gif");
    this.buttonDeletePortlet = new JButton(new ImageIcon(url));
    this.buttonDeletePortlet.addActionListener(this);
    this.buttonDeletePortlet.setToolTipText(this.getGui().getController().notifyTranslation("com.genia.portlet.editor.gui.MainToolBar.buttonDeletePortlet"));
    this.add(this.buttonDeletePortlet);

    this.addSeparator(new Dimension(AbstractGUI.TOOLBAR_SEPARATOR_WIDTH, 16));

    url = PortletEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveUp.gif");
    this.buttonMoveUp = new JButton(new ImageIcon(url));
    this.buttonMoveUp.addActionListener(this);
    this.buttonMoveUp.setToolTipText(this.getGui().getController().notifyTranslation("com.genia.portlet.editor.gui.MainToolBar.buttonMoveUp"));
    this.add(this.buttonMoveUp);

    url = PortletEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveDown.gif");
    this.buttonMoveDown = new JButton(new ImageIcon(url));
    this.buttonMoveDown.addActionListener(this);
    this.buttonMoveDown.setToolTipText(this.getGui().getController().notifyTranslation("com.genia.portlet.editor.gui.MainToolBar.buttonMoveDown"));
    this.add(this.buttonMoveDown);

    url = PortletEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveRight.gif");
    this.buttonMoveRight = new JButton(new ImageIcon(url));
    this.buttonMoveRight.addActionListener(this);
    this.buttonMoveRight.setToolTipText(this.getGui().getController().notifyTranslation("com.genia.portlet.editor.gui.MainToolBar.buttonMoveRight"));
    this.add(this.buttonMoveRight);

    url = PortletEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveLeft.gif");
    this.buttonMoveLeft = new JButton(new ImageIcon(url));
    this.buttonMoveLeft.addActionListener(this);
    this.buttonMoveLeft.setToolTipText(this.getGui().getController().notifyTranslation("com.genia.portlet.editor.gui.MainToolBar.buttonMoveLeft"));
    this.add(this.buttonMoveLeft);
  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  @Override
  public void actionPerformed(ActionEvent e)
  {
    super.actionPerformed(e);
    Object source = e.getSource();
    if (source.equals(this.buttonAddPortlet)) {
      this.getGui().addSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonDeletePortlet)) {
      this.getGui().deleteSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveUp)) {
      this.getGui().moveUpOrRightSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveDown)) {
      this.getGui().moveDownOrLeftSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveRight)) {
      this.getGui().moveUpOrRightSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveLeft)) {
      this.getGui().moveDownOrLeftSubElement(this.getGui().getSelectedElement());
    }
  }



  /**
   * Refresh the access ti the items.
   */
  @Override
  public void refresh()
  {
    super.refresh();
    PortletModel model = this.getGui().getSelectedElement();
    if (model == null) {
      this.buttonAddPortlet.setEnabled(false);
      this.buttonDeletePortlet.setEnabled(false);
      this.buttonMoveUp.setEnabled(false);
      this.buttonMoveDown.setEnabled(false);
      this.buttonMoveRight.setEnabled(false);
      this.buttonMoveLeft.setEnabled(false);
    }
    else {
      if (model instanceof ContainerPortletModel) {
        ContainerPortletModel containerPortletModel = (ContainerPortletModel) model;
        int nbDisplayedSubPortlets = containerPortletModel.getNbSubPortlets();
        if (nbDisplayedSubPortlets >= containerPortletModel.getMaxNbSubPortlets()) {
          this.buttonAddPortlet.setEnabled(false);
        }
        else {
          this.buttonAddPortlet.setEnabled(true);
        }
      }
      else {
        this.buttonAddPortlet.setEnabled(false);
      }
      if (model.isSubElement()) {
        this.buttonDeletePortlet.setEnabled(true);
        if (LayoutDirection.HORIZONTAL.equals(model.getParentDirection())) {
          this.buttonMoveUp.setEnabled(false);
          this.buttonMoveDown.setEnabled(false);
          this.buttonMoveRight.setEnabled(true);
          this.buttonMoveLeft.setEnabled(true);
        }
        else {
          this.buttonMoveUp.setEnabled(true);
          this.buttonMoveDown.setEnabled(true);
          this.buttonMoveRight.setEnabled(false);
          this.buttonMoveLeft.setEnabled(false);
        }
      }
      else {
        this.buttonDeletePortlet.setEnabled(false);
        this.buttonMoveUp.setEnabled(false);
        this.buttonMoveDown.setEnabled(false);
        this.buttonMoveRight.setEnabled(false);
        this.buttonMoveLeft.setEnabled(false);
      }
    }
  }

}
